package web.controller;

import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import spider.entitys.Chapter;
import spider.entitys.ChapterDetail;
import spider.interfaces.IChapterDetailSpider;
import spider.interfaces.IChapterSpider;
import spider.util.ChapterDetailSpiderFactory;
import spider.util.ChapterSpiderFactory;
import web.entitys.JSONResponse;
import web.entitys.PageResponse;
import web.service.NovelService;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.function.DoubleToIntFunction;

/**
 * @author  yb
 * on 2017/8/17.
 **/
@Controller
public class NovelController {
    @Resource
    private NovelService service;

    @RequestMapping( value = "/test/chapters.do", method = RequestMethod.GET )
    @ResponseBody
    public JSONResponse getsChapter( String url ) {
        IChapterSpider spider = ChapterSpiderFactory.GetChapterSpider( url );
        List< Chapter > chapters = spider.getChapter( url );
        return JSONResponse.sucess( chapters );
    }

    @RequestMapping( value = "/test/chapterDetail.do", method = RequestMethod.GET )
    @ResponseBody
    public JSONResponse getChapterDetail( String url ) {
        try {
            System.out.println( new String( url.getBytes( "ISO-8859-1" ), "UTF-8" ) );
        } catch ( UnsupportedEncodingException e ) {
            System.out.println( "这里出问题了   novel.web.controller.NovelController.getChapterDetail" );
        }
        IChapterDetailSpider spider = ChapterDetailSpiderFactory.GetChapterDetailSpider( url );
        ChapterDetail detail = spider.getChapterDetail( url );
        return JSONResponse.sucess( detail );
    }

    @RequestMapping( value = "/novelSearch.do", method = RequestMethod.POST )
    @ResponseBody
    public PageResponse getsNovelByKeyword( String keyword, Integer page, Integer rows ) throws UnsupportedEncodingException {
        PageHelper.startPage( page,rows );
        return PageResponse.sucess( service.getNovelByKeyWord( keyword,page,rows ) );
    }

    /**
     * keyword = new String( keyword.getBytes("ISO-8859-1"),"UTF-8" );
     */
    @RequestMapping( value = "/novelSearch2.do", method = RequestMethod.POST )
    @ResponseBody
    public JSONResponse getsNovelByKeyword( String keyword, int platformId ) throws UnsupportedEncodingException {
        return JSONResponse.sucess( service.getNovelByKeyWord( keyword, platformId ) );
    }

    @RequestMapping( value = "/chapterList.do", method = RequestMethod.GET )
    public ModelAndView showChapterList( String url ) {
        ModelAndView view = new ModelAndView();
        view.setViewName( "/chapterList" );
        view.getModel().put( "chapters", ChapterSpiderFactory.GetChapterSpider( url ).getChapter( url ) );
        view.getModel().put( "baseUrl", url );
        return view;
    }

    @RequestMapping(value = "/chapterDetail.do",method = RequestMethod.GET)
    public ModelAndView showChapterDetail(String url,String baseUrl){
        ModelAndView view = new ModelAndView();
        try {
            view.setViewName( "/chapterDetail" );
            ChapterDetail detail = ChapterDetailSpiderFactory.GetChapterDetailSpider( url ).getChapterDetail( url );
            detail.setContent( detail.getContent().replaceAll( "\n","<br/>" ) );
            view.getModel().put( "chapterDetail", detail);
            view.getModel().put( "baseUrl",baseUrl );
            view.getModel().put( "isSuccess",true );
        }catch ( Exception e ){
            e.printStackTrace();
            view.getModel().put( "isSuccess",false );
        }
        return view;
    }
}
